#include "CPistolBullet.h"
#include "..\CCyberSpaceState.h"
#include "..\CGame.h"
#include "..\\Messaging\CSGP_EventSystem.h"

CPistolBullet::CPistolBullet(void)
{

	// Load textures and sounds

	SetWidth(32);
	SetHeight(32);
	m_fDeathTimer = 0.0f;

	CSGP_EventSystem::GetInstance()->RegisterClient("destroy.all.bullets", this);
	m_nBulletID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_PistolBullet.png");
}

CPistolBullet::~CPistolBullet(void)
{
	//if(m_pOwner)
	//	m_pOwner->Release();

	CSGP_EventSystem::GetInstance()->UnregisterClient("destroy.all.bullets", this);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBulletID);
}

/*void CPistolBullet::SetOwner(CBase* pOwner)
{
	if(m_pOwner)
	{
		m_pOwner->Release();
	}

	m_pOwner = pOwner;

	if(m_pOwner)
		m_pOwner->AddRef();
}*/

void CPistolBullet::Update(float fElapsedTime)
{
	CBase::Update(fElapsedTime);

	m_fDeathTimer += fElapsedTime;

	if(m_fDeathTimer > 2.5f)
	{
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyPlayerBulletMessage(this));
	}

	//// Temporary collision bounding
	//if(GetPosX() < 0 || GetPosX() > CGame::GetInstance()->GetScreenWidth()
	//	|| GetPosY() < 0 || GetPosY() > CGame::GetInstance()->GetScreenHeight() - GetHeight())
	//{
	//	// Destroy the bullet
	//	CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyPlayerBulletMessage(this));
	//}

}

void CPistolBullet::Render(void)
{
	RECT drawRect = GetCollisionRect();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();

	CSGD_TextureManager::GetInstance()->Draw(m_nBulletID, drawRect.left, drawRect.top, 1.0f, 1.0f);

	// Draw collision box
	//CSGD_Direct3D::GetInstance()->DrawRect(drawRect, 0, 255, 0);
}

RECT CPistolBullet::GetCollisionRect(void)
{
	RECT rIntersect;
	rIntersect.left = long(GetPosX());
	rIntersect.top = long(GetPosY());
	rIntersect.right = long(rIntersect.left + GetWidth());
	rIntersect.bottom = long(rIntersect.top + GetHeight());

	return rIntersect;
}

bool CPistolBullet::CheckCollision(IBaseInterface* pBase)
{

	return false;
}

void CPistolBullet::HandleEvent(CEvent* pEvnt)
{
	if(pEvnt->GetEventID() == "destroy.all.bullets")
	{
		// Get the dead enemy
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyPlayerBulletMessage(this));
	}
}